﻿#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QKeyEvent>
#include <QDateTime>
#include <QDebug>
#include <QCameraInfo>
#include <QBoxLayout>
#include <QDir>
#include <QMessageBox>
#include "windows.h"

int g_screenWidth = 0;
int g_screenHeight = 0;

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    desktopWidget = QApplication::desktop();
    QRect applicationRect = desktopWidget->screenGeometry();
    g_screenWidth = applicationRect.width();
    g_screenHeight = applicationRect.height();
    qDebug() << u8"resolution: width=" << g_screenWidth << "height=" << g_screenHeight;
    //框坐标及大小
    int cx,cy,cwid,chei;
    cwid = g_screenWidth * 0.8;
    chei = g_screenHeight * 0.8;
    cx = (g_screenWidth - cwid)/2;
    cy = (g_screenHeight - chei)/2;
    ui->groupBox->setGeometry(cx,cy,cwid,chei);
    ui->groupBox->setStyleSheet("background-color:transparent;border-style:solid;border-width:4px;border-color:red;");
    ui->groupBox->hide();
    ui->label->setFixedSize(500,100);
    ui->label->setStyleSheet("background-color: black; font-size: 80px; font-weight:bold; text-align:center; color: yellow;");
    //
    m_temptime = 0;
    ui->edit_keyfocus->setMaximumSize(0, 1);
    ui->btn_close->setGeometry(0,g_screenHeight-50,100,100);
    //
    m_cameraView = new QCameraViewfinder(this);
    m_camera = new QCamera(this);
    m_camera->setViewfinder(m_cameraView);
    m_capture = new QCameraImageCapture(m_camera);
    QCameraViewfinderSettings settings;
    //2592,1944
//    settings.setResolution(1920, 1080);
    settings.setResolution(2592, 1944);
    m_camera->setViewfinderSettings(settings);

    qDebug() << u8"camera resolution" << settings.resolution();

    //
//    QCameraFocus *camerafocus = m_camera->focus();
//    if(camerafocus)
//    {
//        QCameraFocus::FocusModes focusmode = camerafocus->focusMode();
//        qDebug() << focusmode;
//        camerafocus->setFocusMode(QCameraFocus::ManualFocus);
//        focusmode = camerafocus->focusMode();
//        qDebug() << focusmode;
//    }
    connect(m_capture,&QCameraImageCapture::imageCaptured,this,&MainWindow::capturedisplayImg,Qt::QueuedConnection);
    //
    QHBoxLayout *H = new QHBoxLayout(ui->centralWidget);
    H->addWidget(m_cameraView);
    H->setMargin(0);
    H->setSpacing(0);
    ui->groupBox->raise();
    ui->btn_close->raise();
    ui->btn_close->setStyleSheet("background-color:transparent");
    ui->label->raise();
    ui->label->hide();
    //
    m_camera->start();
    //
    ui->edit_keyfocus->setFocus();
    QObject::connect(&m_timer, &QTimer::timeout, [=] () {
        focusSet();
    });
    m_timer.start(5000);
    QObject::connect(&m_timer1, &QTimer::timeout, [=] () {
        ui->label->hide();
    });
    m_timer1.setSingleShot(true);
}

MainWindow::~MainWindow()
{
    m_camera->stop();
    delete ui;
}

void MainWindow::keyPressEvent(QKeyEvent *e)
{
    if((QDateTime::currentMSecsSinceEpoch() - m_temptime) < 50)
        return;
    m_temptime = QDateTime::currentMSecsSinceEpoch();
    ui->edit_keyfocus->setFocus();
    switch(e->key())
    {
    case Qt::Key_Up:
    case Qt::Key_PageUp:
    {
        qDebug() << u8"上";
        m_capture->capture(u8"tmp.jpg");
    }
        break;
    case Qt::Key_Down:
    case Qt::Key_PageDown:
    {
        qDebug() << u8"下";
        m_capture->capture(u8"tmp.jpg");
    }
        break;
    default:break;
    }
}

void MainWindow::capturedisplayImg(int, QImage image)
{
    QString strCurDate = QDateTime::currentDateTime().toString(Qt::ISODate);
    strCurDate = strCurDate.replace("-","").replace("T","").replace(":","");
    QString dirName = strCurDate.left(8);
    QString dirCur = QDir::currentPath();
    dirCur = dirCur.left(dirCur.lastIndexOf("/"));
    QString szFstAbsoluteDir = QString(u8"%1/照片%2").arg(dirCur).arg(dirName);
    QDir dir(szFstAbsoluteDir);
    if(!dir.exists())
        dir.mkpath(szFstAbsoluteDir);
    QString strSay = u8"";
    int nRandom = qrand()%5;
    qDebug() << nRandom;
    switch(nRandom)
    {
    case 0:{strSay = u8"拍照成功 very good";} break;
    case 1:{strSay = u8"拍照成功 great";} break;
    case 2:{strSay = u8"拍照成功 nice";} break;
    case 3:{strSay = u8"拍照成功 Just wonderful";} break;
    case 4:{strSay = u8"拍照成功 amazing";} break;
    default:{strSay = u8"拍照成功,thank you baby!";} break;
    }
    strSay = u8"成功";
    QString imgpath = szFstAbsoluteDir.append("/") + strCurDate+".jpg";
    qDebug() << imgpath.toUtf8().data();
    if(image.save(imgpath,"JPG", 90)) {
        m_speek.say(strSay);
        ui->label->setText(u8"拍照成功!");
        int x = this->width()/2 - ui->label->width()/2;
        int y = this->height()/2 - ui->label->height()/2;
        ui->label->move(x,y);
        ui->label->show();
        m_timer1.start(1000);
    }else{
        m_speek.say(u8"很遗憾，拍照失败拍照失败，赶紧叫人！");
        ui->label->setText(u8"拍照失败!");
        int x = this->width()/2 - ui->label->width()/2;
        int y = this->height()/2 - ui->label->height()/2;
        ui->label->move(x,y);
        ui->label->show();
        m_timer1.start(1000);
    }
}

void MainWindow::on_btn_close_clicked()
{
    QMessageBox msgBox;
    msgBox.setWindowTitle("确认退出程序吗？");
    msgBox.setText("点击确认后，将推出程序！");
    QPushButton *bok = msgBox.addButton(tr("确定"),QMessageBox::NoRole);
    QPushButton *bcancel = msgBox.addButton(tr("取消"),QMessageBox::NoRole);
    msgBox.exec();
    if(bcancel == msgBox.clickedButton())
        return;
    if(bok == msgBox.clickedButton())
    {
        this->close();
    }
    ui->edit_keyfocus->setFocus();
}

void MainWindow::focusSet()
{
    WId hwnd = this->winId();
    HWND hCurWnd = NULL;
    DWORD dwMyID;
    DWORD   dwCurID;
    hCurWnd = ::GetForegroundWindow();
    dwMyID = ::GetCurrentThreadId();
    dwCurID = ::GetWindowThreadProcessId(hCurWnd, NULL);
    ::AttachThreadInput(dwCurID, dwMyID, TRUE);
    ::SetForegroundWindow((HWND)hwnd);
    ::AttachThreadInput(dwCurID, dwMyID, FALSE);
    //
    ui->edit_keyfocus->setFocus();
}
